//
//  KLShopMenuControl.m
//  KunLun
//
//  Created by Mac on 2017/11/28.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "KLShopMenuControl.h"
@interface KLShopMenuControl()
@property(nonatomic, strong) NSMutableArray                     *controlArray;
@end
@implementation KLShopMenuControl

- (instancetype)initWithFrame:(CGRect)frame titles:(NSArray *)titles subTitles:(NSArray *)subTitles subColor:(UIColor *)subColor{
    if (self == [super initWithFrame:frame]) {
        self.controlArray = [NSMutableArray new];
        CGFloat normalWith = self.width / titles.count;
        for (int i = 0; i < titles.count; i++) {
            UIControl * contrl = [self controlWithFrame:CGRectMake(normalWith * i, 0,normalWith, self.height) title:titles[i] subTitle:subTitles[i] subColor:subColor];
            contrl.tag = i;
            if (i == 0) {
               [self onSlectedControl:contrl];
            }
            [self.controlArray addObject:contrl];
            [self addSubview:contrl];
        }
    }
    return self;
}
- (UIControl*)controlWithFrame:(CGRect)frame title:(NSString *)title subTitle:(NSString *)subTitle subColor:(UIColor *)subColor{
    UIControl * control = [[UIControl alloc] initWithFrame:frame];
    control.backgroundColor = [UIColor whiteColor];
    STLabel *titleLable = [[STLabel alloc] initWithFrame:CGRectMake(0, 0, 80, 30)
                                                text:title
                                           textColor:KL_firstTextColor
                                                font:15
                                         isSizetoFit:NO
                                       textAlignment:NSTextAlignmentCenter];
    titleLable.centerX = control.width / 2;
    titleLable.backgroundColor = KL_backgroundColor;
    titleLable.layer.cornerRadius = 5;
    titleLable.clipsToBounds = YES;
    titleLable.tag = 10000;
    [control addSubview:titleLable];
    
    STLabel *subtitleLable = [[STLabel alloc] initWithFrame:CGRectMake(0, titleLable.bottom + 3, control.width, 20)
                                                   text:subTitle
                                              textColor:subColor
                                                   font:15
                                            isSizetoFit:NO
                                          textAlignment:NSTextAlignmentCenter];
    subtitleLable.centerX = control.width / 2;
    subtitleLable.tag = 10001;
    [control addSubview:subtitleLable];
    
    [control addTarget:self action:@selector(onSlectedControl:) forControlEvents:UIControlEventTouchUpInside];
    return control;
}

#pragma mark --Action Method
- (void)onSlectedControl:(UIControl*)sender{

    
    if (self.canMutipleSelected) {
        sender.selected = !sender.selected;
        [self updateColroWithCntrol:sender];
    }else{
        for (UIControl * contrl in self.controlArray) {
            contrl.selected = NO;
            [self updateColroWithCntrol:contrl];
        }
        sender.selected = YES;
        [self updateColroWithCntrol:sender];
    }
}
- (void)updateColroWithCntrol:(UIControl*)control{
    STLabel * label = [control viewWithTag:10000];
    if (control.selected) {
        label.backgroundColor = KL_BlueBackGroundColor;
        label.textColor = [UIColor whiteColor];
    }else{
        label.backgroundColor = KL_backgroundColor;
        label.textColor = KL_firstTextColor;
    }
}
@end
